package com.tongna;

import com.tongna.tenant.CurrentTenantIdentifierHolder;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * TenantDemo
 *
 * @author 张浩伟
 * @version 1.01 2018年02月09日
 */
public class TenantInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取域名
        String serverName = request.getServerName();
        if (serverName != null) {
            CurrentTenantIdentifierHolder.set(serverName);
        } else {
            throw new RuntimeException("未找到主机名");
        }
        request.getSession().setAttribute("admin", "www.domain.com".equals(serverName));
        request.getSession().setAttribute("domain", serverName);
        return true;
    }
}
